import { useIntersectionObserver } from '@vueuse/core'
import defaultImg from '@/assets/images/200.png'

const directivesPlugins = {
  install(app) {
    app.directive('lazyLoad', {
      mounted(el, binding) {
        const { stop } = useIntersectionObserver(
          el,
          ([{ isIntersecting }], observerElement) => {
            el.onerror = () => {
              el.src = defaultImg
            }
            if (isIntersecting) {
              el.src = binding.value
              stop()
            }
          },
          {
            threshold: 0
          }
        )
      }
    })
  }
}
export default directivesPlugins